//
//  ConfirmSendGoodsCtr.swift
//  smosky
//
//  Created by zhuxietong on 2018/8/8.
//  Copyright © 2018年 tianrun. All rights reserved.
//

import Foundation
import soapp
import Eelay
import AsyncDisplayKit


class ConfirmSendGoodsCtr:ASColListController{
    
    var sections:TP.section{
        get{
            //            let goods = DataNode.path(node: "goods", mo.model, value: NSMutableArray())
            
            let sec:TP.section = [
                [
                    ["cellID":"goods","__model":mo.model],
                    ["cellID":"note","info":"请输入快递信息，方便卖家跟踪退货物流".loc],
                    
                    ],
                [
                    [
                        "title":"快递公司".loc,
                        "cellID":"com",
                        FK.fields:[
                            [
                                FK.placeholder:"选择快递公司".loc,
                                FK.field:StrField(id: "exp", defaultV: nil, need: true, hiden: false, rule:
                                    JoRule(reg: "^[\\s\\S]{1,200}$", nil_msg: "选择快递公司".loc, err_msg: "选择快递公司".loc)),
                            ]
                        ]
                    ],
                    ["title":"快递单号".loc,
                     "cellID":"no",
                     FK.fields:[
                        [
                            FK.placeholder:"请输入快递单号".loc,
                            FK.field:StrField(id: "expresssn", defaultV: nil, need: true, hiden: false, rule:
                                JoRule(reg: "^[\\s\\S]{5,40}$", nil_msg: "请填写快递单号".loc, err_msg: "检查快递单号".loc)),
                            ],
                        ]
                    ],
                    ],
                [
                    ["cellID":"blank","height":"40","hex":UIColor.table_bg_hex],
                    ]
            ]
            return sec
        }
    }
    
    let selector:TP.selector = [
        "cellID<note>":NoteCell.self,
        "cellID<blank>":BlankNode.self,
        "cellID<goods>":SaleAfter.GoodsCell.self,
        "cellID<no>":FmTextNode.self,
        "cellID<com>":FmTypeNode.self

    ]
    
    
    var returnID:String{
        get{
            return ""
        }
    }
    
    let bottomBar = SubmitBar()
    override func layCollection(collectionView: UIView) {
        
   
        jo_contentView.eelay = [
            [bottomBar,[ee.L.B.R]],
            [layCollection,[bottomBar,ee.T,ee.B],[ee.T.L.R]],
        ]
        bottomBar.controller = self
        bottomBar.nextAction = {[weak self] in
            self?.submitAction()
        }
    }
    
    func submitAction() {
        let info = JoPaw.paw(sectionObj: collectionNode.model)
        if info.validate
        {
            let exp = info.values.mutable_dictionary["exp",""].JsonDictionary ?? NSMutableDictionary()
            
            
            var values = info.values
            values.removeValue(forKey: "exp")
            values["express"] = exp["express",""]
            values["expresscom"] = exp["name",""]
            values["refundid"] = mo.model["id",""]
            values["orderid"] = mo.model["orderid",""]
            
            
            
            
            
            //            print("+++DD|\(mo.model)")
            //            print("++++++DKK|\(values)")
            //
            
            weak var wself = self
            HUD.share.show().mode(.indeterminate)
            JoTask().post.log.log_response()._surl(.确认退货发货).params(values).json_handle { (status, msg, obj, resp) in
                HUD.share.hide(animated: true)
                if status.isSuccess{

                    __post(notify: Goods.Notice.changeAfterStatus)
                    wself?.navigationController?.popViewController(animated: true)
                    "操作成功".loc.alert()
                    return
                }
                msg.alert()
                }.run()
        }
    }
    
    
    override func viewDidLoad() {
        
        collectionNode.collectionViewClass = AvoidCollectionView.self
        super.viewDidLoad()
//        tableNode.view.setupTPKeyboardAvoiding()
        title = "退货信息".loc
        load(sections: sections, selector: selector)
        //        mo.detail = mo.model
        collectionNode.backgroundColor = UIColor.table_bg_color
    }
    deinit {
//        tableNode.view.clearTPKeyboardAvoiding()
    }
    
}

extension ConfirmSendGoodsCtr{
    
    class SubmitBar: EeBottomBar {
        
        override var contentHeight: CGFloat{
            get{
                return 60
            }
        }
        let sbBt = UIButton()
        
        var nextAction:()->Void = { }
        
        override func addLayoutRules() {
            let line = UIView()
            contentView.eelay = [
                [line,[ee.T.L.R],"\(TP.separatorHeight)"],
                [sbBt,[ee.L.R.Y,[AppTheme.edge,-AppTheme.edge,0]],"36"],
            ]
            line.backgroundColor = UIColor.separator_color
            sbBt.ui.buttonColor(color: ___shop.tint).ncolor(hex: "#fff").font(size: 13).padding(insets: [6,12]).ntitle(title: "提交退货申请".loc).set(radius: 4, width: nil, color: nil)
            sbBt.addTarget(self, action: #selector(confirmAction), for: .touchUpInside)
        }
        
        @objc func confirmAction(){
            nextAction()
        }
    }
    
    
    class NoteCell: JoCellNode {
        let infoL = ASTextNode2()
        override func mo_model_reload() {
            infoL.attributedText = mo.model["info",""].ui.font13.cl_666.astring
        }
        override func specGetter() -> _Spec {
            return .inset([], [16,AppTheme.edge,8,AppTheme.edge], child: infoL.Spec)
        }
    }
    
    
    class FmTypeNode: ASFieldsCell {
        let titleL = JoTextNode()
        var field = ASFieldNode<PushSelectorField>()
        
        
        override func addLayoutRules() {
            self.fields = [field]
        }
        
        override func mo_model_reload() {
            super.mo_model_reload()
            titleL.attributedText = (mo.model["title",""] + ":").ui.font13.cl_333.astring
        }
        
        override func specGetter() -> _Spec {
            let content = _Spec.stack(["h|6",&-.center,[12,AppTheme.edge]], childs: [
                titleL[80],
                field[_grow(1),"24"],
                ])
            return content
        }
    }
    
    class FmTextNode: ASFieldsCell {
        let titleL = JoTextNode()
        var field = ASFieldNode<FTextField>()
        
        
        override func addLayoutRules() {
            self.fields = [field]
        }
        
        override func mo_model_reload() {
            super.mo_model_reload()
            titleL.attributedText = (mo.model["title",""] + ":").ui.font13.cl_333.astring
        }
        
        override func specGetter() -> _Spec {
            let content = _Spec.stack(["h|6",&-.center,[12,AppTheme.edge,12,10]], childs: [
                titleL[80],
                field[_grow(1),"24"],
                ])
            return content
        }
    }
    
}

